package day240524.work;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

public class Test {
    private static final Scanner scanner= new Scanner(System.in);
    private static  List<Student> student =new ArrayList<Student>();
    private static final Properties STUDENTS_PROPERTIES = new Properties();
    private static final String FILE_NAME = "src/day240524/work/Xxx.properties";


    public static void main(String[] args) throws IOException {
        loadFromFile();
        while (true) {
            System.out.println("--- --- --- --- --- ---");
            System.out.println("请输入操作编号：");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生信息");
            System.out.println("4. 查询特定学生信息");
            System.out.println("5. 查看所有学生信息");
            System.out.println("6. 退出");
            System.out.println("--- --- --- --- --- ---");

            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    queryStudent();
                    break;
                case 5:
                    listStudents();
                    break;
                case 6:
                    storeToFile();
                    System.out.println("已退出学生管理系统。");
                    /*System.exit(0);
                    break;*/
                    return;
                default:
                    System.out.println("无效的操作编号！");
            }
        }
    }
    private static void loadFromFile() throws IOException {
        STUDENTS_PROPERTIES.load(new FileInputStream(FILE_NAME));
        Set<Object> objects = STUDENTS_PROPERTIES.keySet();
        for (Object next : objects) {
            String name = (String) next;
            String studentString = STUDENTS_PROPERTIES.getProperty(name);
            String[] split = studentString.split(",");
            student.add(new Student(split[0], Integer.parseInt(split[1]),split[2]));
        }
    }

    private static void storeToFile() throws IOException {
        STUDENTS_PROPERTIES.clear();
        for (Student student : student) {
            String key = student.getName();
            String value = student.getName() + "," + student.getAge() + "," + student.getHometown();
            STUDENTS_PROPERTIES.setProperty(key, value);
        }
        STUDENTS_PROPERTIES.store(new FileOutputStream(FILE_NAME), STUDENTS_PROPERTIES.toString());
    }
    private static void LoadFromFile()throws IOException{

    }

    private static void addStudent() {
        System.out.println("请输入学生姓名：");
        String name = scanner.next();

        System.out.println("请输入学生年龄：");
        int age = scanner.nextInt();

        System.out.println("请输入学生家乡：");
        String gender = scanner.next();

        Student student = new Student(name, age, gender);
        Test.student.add(student);

        System.out.println("添加学生成功！");
    }

    private static void deleteStudent() {
        System.out.println("请输入要删除的学生姓名：");
        String name = scanner.next();

        Student student = findStudentByName(name);
        if (student == null) {
            System.out.println("学生不存在！");
            return;
        }

        Test.student.remove(student);

        System.out.println("删除学生成功！");
    }

    private static void updateStudent() {
        System.out.println("请输入要修改的学生姓名：");
        String name = scanner.next();

        Student student = findStudentByName(name);
        if (student == null) {
            System.out.println("学生不存在！");
            return;
        }

        System.out.println("请输入学生新姓名：");
        String newName = scanner.next();

        System.out.println("请输入学生新年龄：");
        int newAge = scanner.nextInt();

        System.out.println("请输入学生新家乡：");
        String newHometown = scanner.next();

        student.setName(newName);
        student.setAge(newAge);
        student.setHometown(newHometown);

        System.out.println("修改学生成功！");
    }

    private static void queryStudent() {
        System.out.println("请输入要查询的学生姓名：");
        String name = scanner.next();

        Student student = findStudentByName(name);
        if (student == null) {
            System.out.println("学生不存在！");
            return;
        }

        System.out.println(student);
    }

    private static Student findStudentByName(String name) {
        for (Student student : Test.student) {
            if (student.getName().equals(name)) {
                return student;
            }
        }

        return null;
    }

    private static void listStudents() {
        if (student.isEmpty()) {
            System.out.println("当前没有任何学生信息。");
        } else {
            System.out.println("所有学生信息如下:");
            for (Student student : Test.student) {
                System.out.println(student);
            }
        }
    }

}class Student {
    private String name;
    private int age;
    private String hometown;

    public Student(String name, int age, String hometown) {
        this.name = name;
        this.age = age;
        this.hometown = hometown;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getHometown() {
        return hometown;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setHometown(String hometown) {
        this.hometown = hometown;
    }

    @Override
    public String toString() {
        return "姓名: " + name + ", 年龄: " + age + ", 家乡: " + hometown;
    }}

